#!/usr/bin/env expect

set timeout 30
set user toll
set passwd admin

set fid [open hosts.txt r]
while {[gets $fid line] >= 0} {
    set ip [lindex $line 0]

    spawn ssh -l $user $ip
    expect {
        "(yes/no)?" {
            send "yes\n"
            expect "password:"
            send "$passwd\n"
        }
        "password:" {
            send "$passwd\n"
            exp_continue
        }
        "\\\$" {
            send "\n"
        }
        timeout {
            puts "ssh timeout"
        }
        eof {
            puts "ssh eof"
        }
    }

    expect "\\\$" {
        send "cat /home/toll/toller/config/update.cfg\n"
    }

    expect "\\\$" {
        send "sed -i 's/app\\\s*=.*/app = mix/g' /home/toll/toller/config/update.cfg\n"
    }

    expect "\\\$" {
        send "cat /home/toll/toller/config/update.cfg\n"
    }

    expect "\\\$" {
        send "killall myupdate\n"
    }

    expect "\\\$" {
        send "myupdate -config /home/toll/toller/config/update.cfg &\n"
    }

    expect "\\\$" {
        send "exit\n"
    }

    expect eof
}
close $fid





